import 'package:aichat_flutter_wallet/kai_print.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'chain_logo.dart';

class TokenLogo extends StatelessWidget {
  final double size;
  final Token token;
  final double? fontSize;
  final bool showChain;

  const TokenLogo({super.key, required this.token, this.size = 32, this.fontSize, this.showChain = false});

  @override
  Widget build(BuildContext context) {
    Chain chain = token.getChain()!;
    Widget logoWidget;
    if (token.contractType == TokenType.native.name) {
      logoWidget = PublicImage(
        image: token.logo??chain.logo,
        width: size,
        height: size,
      );
      return SizedBox(
        width: size,
        height: size,
        child: ClipOval(
          child: logoWidget,
        ),
      );
    } else if (null != token.logo && token.logo!.isNotEmpty) {
      var currentLogo = "assets/icon/${token.symbol}.png";
      if (token.logo!.indexOf('http') > -1) {
        currentLogo = token.logo!;
        logoWidget = PublicImage(
          image: currentLogo, // 无效的图片 URL
          width: size,
          height: size,
          fit: BoxFit.cover,
        );
      } else {
        logoWidget = Image.asset(
          currentLogo, // 无效的图片 URL
          width: size,
          height: size,
          fit: BoxFit.cover,
          errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
            return Text('Failed to load image'); // 加载失败时返回文本组件
          },
        );
      }
    } else {
      try {
        var image = 'https://img.feixiaohao.hk/logo/1/${token.name.toLowerCase()}.png';
        if (token.symbol.toLowerCase() == 'usdt') {
          image = "https://img.feixiaohao.hk/logo/1/tether.png";
        } else if (token.symbol.toLowerCase() == 'usdc') {
          image = "https://img.feixiaohao.hk/logo/1/usdc.png";
        }
        logoWidget = CachedNetworkImage(
            fit: BoxFit.cover,
            imageUrl: image,
            width: size,
            height: size,
            errorWidget: (context, url, error) => Container(
                  padding: const EdgeInsets.all(2),
                  color: Color.fromRGBO(134, 103, 254, 1),
                  child: Center(
                    child: Text(
                      token.getShowName(),
                      textAlign: TextAlign.center,
                      maxLines: 1,
                      overflow: TextOverflow.clip,
                      style: TextStyle(
                          color: Colors.white, fontSize: fontSize ?? (10 * size.sp / 32), fontWeight: FontWeight.bold),
                    ),
                  ),
                ));
      } catch (e) {
        logoWidget = Container(
          padding: const EdgeInsets.all(2),
          color: Color.fromRGBO(134, 103, 254, 1),
          child: Center(
            child: Text(
              token.getShowName(),
              textAlign: TextAlign.center,
              maxLines: 1,
              overflow: TextOverflow.clip,
              style: TextStyle(
                  color: Colors.white, fontSize: fontSize ?? (10 * size.sp / 32), fontWeight: FontWeight.bold),
            ),
          ),
        );
      }
    }

    Widget networkImageWidget = SizedBox(
      width: size,
      height: size,
      child: ClipOval(
        child: logoWidget,
      ),
    );
    return token.contractType == TokenType.native.name
        ? SizedBox(
            width: size,
            height: size,
            child: ClipOval(
              child: logoWidget,
            ),
          )
        : (showChain
            ? Stack(
                children: [
                  networkImageWidget,
                  Container(
                    margin: EdgeInsets.only(left: size - 10, top: size - 13),
                    padding: const EdgeInsets.all(1.5),
                    decoration: const BoxDecoration(color: Color.fromRGBO(134, 103, 254, 1), shape: BoxShape.circle),
                    child: ChainLogo(chain: chain, size: 12),
                  )
                ],
              )
            : networkImageWidget);
  }
}
